﻿using Lamex.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Common
{
    public class GetCommon
    {
        public static DataTable GetSizeParmList(string DWG_NO, string PART_NO)
        {
            DataTable dataTable = new DataTable();
            string sql = @"SELECT [DWG_NO]
      ,[PART_NO]
      ,[WIDTH_1]
      ,[WIDTH_2]
      ,[DEPTH_1]
      ,[DEPTH_2]
      ,[THICK]
      ,[LENGTH]
      ,[WIDTH]
      ,[DEPTH]
      ,[HEIGHT]
      ,[UPDATE_TIME]
      ,[UPDATE_USER]
  FROM [LamexMESManager].[dbo].[IE_Part_SizeParmList] where DWG_NO='" + DWG_NO + "' AND PART_NO='" + PART_NO + "'";
            dataTable = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            return dataTable;
        }
        public static DataTable GetFeature_Definatian()
        {
            DataTable dataTable = new DataTable();
            string sql = @" SELECT [FEATURE_NAME]    
      ,[UNIT_WORKHOURS]
      ,[ATTACH_WORKHOURS]  FROM [LamexMESManager].[dbo].[IE_Feature_Definatian] ";
            dataTable = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            return dataTable;
        }

        public static DataTable GetWorkCenter_AllowanceFactor()
        {
            DataTable dataTable = new DataTable();
            string sql = @"SELECT  [PROCESS_CODE]
      ,[ALLOWANCE_FACTOR]
      ,[EFFECTIVE_DATE]
      ,[EXPIRY_DATE]
  FROM [LamexMESManager].[dbo].[IE_WorkCenter_AllowanceFactor]";
            dataTable = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            return dataTable;
        }
        /// <summary>
        /// 获取BOM特征适用表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetBOM_NECESSARY()
        {
            string sql = @"SELECT 
       [Operation_Name]
      ,[Material_No]
      ,[Describe]
      ,[Create_TS]
      ,[Create_User]
      ,[Status]
  FROM [LamexMESManager].[dbo].[B_IE_COMPONENT_NECESSARY]";
            DataTable dt = new DataTable();
            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            return dt;
        }
        public static DataTable GetFEATURE_NAME(string PROCESS_CODE)
        {
            string sql = @"SELECT FEATURE_NAME FROM [dbo].[IE_WorkCenter_Feature] 
  WHERE PROCESS_CODE='" + PROCESS_CODE + "'";
            DataTable dt = new DataTable();
            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            return dt;
        }
        //去重DataTable列重复值
        public static DataTable DistinctSomeColumn(DataTable sourceTable, params string[] fieldName)
        {
            if (fieldName == null || fieldName.Length == 0) return sourceTable;
            return sourceTable.AsEnumerable().Distinct(new ColumnEquals(fieldName)).CopyToDataTable();
        }
        public class ColumnEquals : IEqualityComparer<DataRow>
        {
            public ColumnEquals(string[] sArr)
            {
                _sArr = sArr;
            }
            private string[] _sArr;

            public bool Equals(DataRow x, DataRow y)
            {
                return !_sArr.Any(p => !x[p].Equals(y[p]));
            }
            public int GetHashCode(DataRow dr)
            {
                return dr.ToString().GetHashCode();
            }
        }

        public static bool Update_Delete_Table(string strDWG_NO, string strPART_NO, string strFILE_NO, string strREF_MAT)
        {
            //任务清单表
            string sql = @"  UPDATE [LamexMESManager].[dbo].[IE_Part_TaskList] SET FILE_NO='" + strFILE_NO + "',REF_MAT='" + strREF_MAT + "'," +
                "ROUTING_FLAG='',PARM_FLAG='',FEAT_FLAG='',REF_FLAG='',CAL_FLAG='',UPLOAD_FLAG=''" +
                " WHERE DWG_NO='" + strDWG_NO + "' AND PART_NO='" + strPART_NO + "' AND ISNULL(LOCK_FLAG,'')=''";
            //工艺路线表
            sql = sql + ";" + @"DELETE FROM [LamexMESManager].[dbo].[IE_Part_Routing] WHERE DWG_NO='" + strDWG_NO + "' AND PART_NO='" + strPART_NO + "'";
            //尺寸参数表
            sql = sql + ";" + @"DELETE FROM [LamexMESManager].[dbo].[IE_Part_SizeParmList] WHERE DWG_NO='" + strDWG_NO + "' AND PART_NO='" + strPART_NO + "'";
            //部件特征表
            sql = sql + ";" + @"DELETE FROM [LamexMESManager].[dbo].[IE_Part_FeatureList] WHERE DWG_NO='" + strDWG_NO + "' AND PART_NO='" + strPART_NO + "'";
            //周期表
            sql = sql + ";" + @"DELETE FROM [LamexMESManager].[dbo].[IE_Part_CycleTime] WHERE PART_NO='" + strPART_NO + "'";
            //工时表
            sql = sql + ";" + @"DELETE FROM [LamexMESManager].[dbo].[IE_Part_ProcessTime] WHERE DWG_NO='" + strDWG_NO + "' AND PART_NO='" + strPART_NO + "'";
            int i = BaseBuildSql.ExecuteNonQuery(sql);
            if (i > 0)
                return true;
            else
                return false;
        }
    }
}
